from django.contrib.auth.decorators import login_required
from django.shortcuts import render
from LogisticsCard.models import FlowWarning, FlowWarningRecord, CardExpireReminder

@login_required
def flow_warning_list_view(request):
    from LogisticsCard.models import Customer
    customer = Customer.objects.filter(user=request.user).first()
    if customer:
        warnings = FlowWarning.objects.filter(card__carduserrelation__customer=customer, delete_at__isnull=True).order_by('-created_at')
    else:
        warnings = FlowWarning.objects.none()
    from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
    paginator = Paginator(warnings, 10)
    page = request.GET.get('page')
    try:
        page_obj = paginator.page(page)
    except PageNotAnInteger:
        page_obj = paginator.page(1)
    except EmptyPage:
        page_obj = paginator.page(paginator.num_pages)
    return render(request, 'warning/flow_warning.html', {'page_obj': page_obj})

@login_required
def flow_warning_record_list_view(request):
    from LogisticsCard.models import Customer
    customer = Customer.objects.filter(user=request.user).first()
    if customer:
        records = FlowWarningRecord.objects.filter(warning__card__carduserrelation__customer=customer, delete_at__isnull=True).order_by('-triggered_at')
    else:
        records = FlowWarningRecord.objects.none()
    from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
    paginator = Paginator(records, 10)
    page = request.GET.get('page')
    try:
        page_obj = paginator.page(page)
    except PageNotAnInteger:
        page_obj = paginator.page(1)
    except EmptyPage:
        page_obj = paginator.page(paginator.num_pages)
    return render(request, 'warning/flow_warning_record.html', {'page_obj': page_obj})

@login_required
def card_expire_reminder_list_view(request):
    from LogisticsCard.models import Customer
    customer = Customer.objects.filter(user=request.user).first()
    reminders = CardExpireReminder.objects.filter(card__carduserrelation__customer=customer, delete_at__isnull=True).order_by('-expire_date') if customer else CardExpireReminder.objects.none()
    from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
    paginator = Paginator(reminders, 10)
    page = request.GET.get('page')
    try:
        page_obj = paginator.page(page)
    except PageNotAnInteger:
        page_obj = paginator.page(1)
    except EmptyPage:
        page_obj = paginator.page(paginator.num_pages)
    return render(request, 'warning/expire_reminder.html', {'page_obj': page_obj})
